/*********************************************************************************
 *      Copyright:  (C) 2022 zengwen<1825984037@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  mqtt_conf.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(18/07/22)
 *         Author:  zengwen <1825984037@qq.com>
 *      ChangeLog:  1, Release initial version on "18/07/22 14:38:23"
 *                 
 ********************************************************************************/

#include "iniparser.h"
#include "mqtt_conf.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>

/*载入设置参数*/
int get_config(char *path,struct total_data *mqtt,char *platform)
{
	dictionary    *ini = NULL;
	const char    *broker_adress;
	int            broker_port;
	const char    *username;
	const char    *password;
	const char    *clientid;
	const char    *topic;
	int            t_interval;
	const char    *db_file = NULL;
	const char    *log_file = NULL;
	int            level;
	int            size;


	if(!path || !mqtt )
	{
		printf("invalid_argument: %s\n",__FUNCTION__);
		return -1;
	}

	ini = iniparser_load(path);//解析dictionary对象（分配内存）

	if(!ini )
	{
		printf("iniparser_load failed!\n");
		return -1;
	}

	/* 解析alicloud配置信息 */
	if(strcmp(platform, iniparser_getsecname(ini,2)) == 0)//获取dictionary对象的第1个section的名字
	{
		broker_adress = iniparser_getstring(ini, "alicloud:host","null");
		broker_port = iniparser_getint(ini,"alicloud:port",0);
		username = iniparser_getstring(ini,"alicloud:username","null");
		password = iniparser_getstring(ini,"alicloud:pwd","null");
		clientid = iniparser_getstring(ini,"alicloud:id","null");
		topic = iniparser_getstring(ini,"alicloud:topic","null");
	}

	/* 解析tencent配置信息 */
	else if(strcmp(platform, iniparser_getsecname(ini,3)) == 0)
	{
		broker_adress = iniparser_getstring(ini, "tencentcloud:host","null");
		broker_port = iniparser_getint(ini,"tencentcloud:port",0);
		username = iniparser_getstring(ini,"tencentcloud:username","null");
		password = iniparser_getstring(ini,"tencentcloud:pwd","null");
		clientid = iniparser_getstring(ini,"tencentcloud:id","null");
		topic = iniparser_getstring(ini,"tencentcloud:topic","null");
	}

	/* 解析huaweicloud配置信息 */
	else if(strcmp(platform, iniparser_getsecname(ini,4)) == 0)
	{
		broker_adress = iniparser_getstring(ini, "huaweicloud:host","null");
		broker_port = iniparser_getint(ini,"huaweicloud:port",0);
		username = iniparser_getstring(ini,"huaweicloud:username","null");
		password = iniparser_getstring(ini,"huaweicloud:pwd","null");
		clientid = iniparser_getstring(ini,"huaweicloud:id","null");
		topic = iniparser_getstring(ini,"huaweicloud:topic","null");
	}

	strncpy(mqtt->mqtt.brokeraddress,broker_adress,SIZE);
	mqtt->mqtt.brokerport = broker_port;
	strncpy(mqtt->mqtt.username,username,SIZE);
	strncpy(mqtt->mqtt.password,password,SIZE);
	strncpy(mqtt->mqtt.clientid,clientid,SIZE);
	strncpy(mqtt->mqtt.topic,topic,SIZE);

	t_interval = iniparser_getint(ini,"common:t_interval",-1);
	db_file = iniparser_getstring(ini, "common:db_file","null");
	log_file = iniparser_getstring(ini,"logger:log_file","null");
	level = iniparser_getint(ini,"logger:level",0);
	size = iniparser_getint(ini,"logger:size",-1);

	mqtt->common.t_interval = t_interval;
	strncpy(mqtt->common.db_file,db_file,SIZE);
	strncpy(mqtt->common.log_file,log_file,SIZE);
	mqtt->common.level = level;
	mqtt->common.size = size;
	mqtt->platform = platform;


	iniparser_freedict(ini);//释放dictoinary对象（内存）
	return 0;
}
